﻿using LdtDataEngin.ModuleDetail.CommonModule;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.Dudection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ldt.WindowsFormsApp
{
    public partial class RequestForm : UIForm
    {
        private List<RequestDataModel> requestDatas = new List<RequestDataModel>();
        DataContext _dataContext;

        public RequestForm(object requestData, DataContext dataContext)
        {
            InitializeComponent();
            this.requestDatas.Clear();
            this.requestDatas = requestData as List<RequestDataModel>;
            _dataContext = dataContext;
        }

        private void RequestForm_Load(object sender, EventArgs e)
        {
            if (requestDatas!=null && requestDatas.Any())
            {
                DataTypeComboBox.DataSource = requestDatas;
                DataTypeComboBox.SelectedValueChanged -= DataTypeComboBox_SelectedValueChanged;
                DataTypeComboBox.ValueMember = "RequestKey";
                DataTypeComboBox.DisplayMember = "RequestKey";
                DataTypeComboBox.SelectedValueChanged += DataTypeComboBox_SelectedValueChanged;
            }
        }

        private void DataTypeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            RequestUrlRichTextBox.Clear();
            RequestParamRichTextBox.Clear();
            RequestResultRichTextBox.Clear();
            var selectedRequest = this.DataTypeComboBox.SelectedItem as RequestDataModel;
            if (selectedRequest != null)
            {

                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestUrl))
                {
                    RequestUrlRichTextBox.Text = selectedRequest.RequestUrl;
                }


                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestParam))
                {
                    RequestParamRichTextBox.Text = selectedRequest.RequestParam;
                }


                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestResult))
                {
                    RequestResultRichTextBox.Text = selectedRequest.RequestResult;
                }
                rtbRepeat.Text = string.Empty;

                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestXml))
                {
                    rtbXml.Text = FormMain.FormatXml(selectedRequest.RequestXml);
                }
                else
                {
                    rtbXml.Text = string.Empty;
                }

                if (!string.IsNullOrWhiteSpace(selectedRequest.ResposeXml))
                {
                    rtbUpdateXml.Text = FormMain.FormatXml(selectedRequest.ResposeXml);
                }
                else
                {
                    rtbUpdateXml.Text = string.Empty;
                }
            }
        }

        /// <summary>
        /// 重新执行请求优选按钮方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRepeat_Click(object sender, EventArgs e)
        {
            var selectedRequest = this.DataTypeComboBox.SelectedItem as RequestDataModel;
            string url = selectedRequest.RequestUrl;
            string postData =selectedRequest.RequestParam;
            Dictionary<string, string> deduceHeaders = new Dictionary<string, string>();
            var deduceTraceId = Snowflake.Instance().GetId().ToString();
            deduceHeaders.Add("traceId", deduceTraceId);
            try
            {
                string  re = HttpHelp.HttpPostJson(url, postData, deduceHeaders);
                if (!string.IsNullOrWhiteSpace(re))
                {
                    rtbRepeat.Text = re;
                    JArray ja = (JArray)JsonConvert.DeserializeObject(re);
                    if (ja.Count > 0)
                    {
                        List<string> cacseDeduceResults = new List<string>();
                        Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
                        cacseDeduceResults.Add(re);
                        result.Add("case", cacseDeduceResults);
                        DeduceInputModel sendParameter= JsonConvert.DeserializeObject<DeduceInputModel>(selectedRequest.RequestParam.ToString());
                        string type = selectedRequest.RequestKey.Split('_').Any()? selectedRequest.RequestKey.Split('_')[0]:"";
                        if (!string.IsNullOrWhiteSpace(type))
                        {
                           int flag= DataDeduceDetail.GetFlag(type);
                            string resXml =new DataDeduceDetail(_dataContext, "").UpdateXml(result, flag, sendParameter.outParameter.FirstOrDefault(a => a.code == "CaseCauseID").Value, rtbXml.Text);
                            if (!string.IsNullOrEmpty(resXml))
                            {
                                rtbUpdateXml.Text = FormMain.FormatXml(resXml);
                                MessageBox.Show("重新执行完成！");
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("重新执行发生错误：" + ex.StackTrace);
            }

        }
    }
}
